package itesm.coler.data;

import itesm.coler.data.DAOUtil;
import itesm.coler.models.Message;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

public class MySQLMessageDAO implements MessageDAO{

	Connection connection = null;
	
	public MySQLMessageDAO()
	{
		
	}
	
	public Connection getConnection(){
		if (connection == null)
		    connection = MySQLDAOFactory.createConnection();
		return connection;
	}
	
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	
	@Override
	public ArrayList<Message> findAll() throws DAOException {
		// implement search messages here using the
	    // supplied criteria.
	    // Alternatively, implement to return a Collection 
	    // of Transfer Objects.
         PreparedStatement findMessage = null;
         ResultSet results= null;
		 ArrayList<Message> messageList = null;
		 try {
		       connection = getConnection();
		       findMessage = connection.prepareStatement("SELECT * FROM message");
		       synchronized(findMessage) {
		       // obtain list of messages	  
		    	  results = findMessage.executeQuery();
		       // get rows data{
		    	  messageList = new ArrayList<Message>();
		         while (results.next()) {
		        	 Message message = mapResultSetToObject(results);		
		        	 messageList.add(message);	   
		         }
		       }
		   } catch (SQLException e){
			   throw new DAOException(e);
		   } finally {
				DAOUtil.close(connection,findMessage,results);
		   }
		  return messageList;
	}

	@Override
	public Message findMessageByMessageId(int messageId) throws DAOException {
		// Implement find a Message here using supplied
		 // argument values as search criteria
		 // Return a Transfer Object if found,
		 // return null on error or if not found
       PreparedStatement findMessage = null;
       ResultSet results= null;
       Message message = null;
		try{
				  connection = getConnection();
				  if (connection!=null){
					  findMessage = connection.prepareStatement("select * from message where messageid = ?");
					  findMessage.setInt(1,messageId);
					  results = findMessage.executeQuery();
					  if (results.next()) {
						  message = mapResultSetToObject(results);						  			  	
					  }
				  }
		}catch(SQLException e){
			throw new DAOException(e);
		} finally {
			DAOUtil.close(connection,findMessage,results);
		}
		return message;
	}

	@Override
	public ArrayList<Message> findMessageByMessageTypeId(String messageTypeId)
			throws DAOException {
		// implement search messages here using the
	    // supplied criteria.
	    // Alternatively, implement to return a Collection 
	    // of Transfer Objects.
         PreparedStatement findMessagesByTypeId= null;
         ResultSet results= null;
		 ArrayList<Message> messageList = null;
		 try {
		       connection = getConnection();
		       findMessagesByTypeId = connection.prepareStatement("SELECT * FROM message where messagetypeid = ?");
		       synchronized(findMessagesByTypeId) {
		    	   findMessagesByTypeId.setString(1,messageTypeId);
		       // obtain list of messages	  
		    	  results = findMessagesByTypeId.executeQuery();
		       // get rows data{
		    	  messageList = new ArrayList<Message>();
		         while (results.next()) {
		        	 Message message = mapResultSetToObject(results);		
		        	 messageList.add(message);	   
		         }
		       }
		   } catch (SQLException e){
			   throw new DAOException(e);
		   } finally {
				DAOUtil.close(connection,findMessagesByTypeId,results);
		   }
		  return messageList;
	}

	public Message mapResultSetToObject(ResultSet results) throws SQLException {
		  System.out.println("Voy a mapear el objeto message");
		  Message message = new Message();
		  message.setMessageId(results.getInt("messageid"));
		  message.setMessageText(results.getString("messagetext"));
		  message.setMessageKind(results.getString("messagekind"));
		  message.setMessageTypeId(results.getString("messagetypeid"));
		  message.setMessageClassif(results.getString("messageclassif"));
		  message.setActive(results.getBoolean("active"));
		  
	
		  return message;
	 }
}
